//
//  ReservationDepartListViewController.m
//  homeland
//
//  Created by SunShine on 13-10-29.
//  Copyright (c) 2013年 xzysun. All rights reserved.
//

#import "ReservationDepartListViewController.h"
#import "PolicyAdvisoryCell.h"
#import "ReservationListViewController.h"
#import "ReservationRecordListViewController.h"

@interface ReservationDepartListViewController ()
@property (strong, nonatomic) ReservationService *dataService;
@end

@implementation ReservationDepartListViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"按主管部门筛选";
    self.navigationItem.rightBarButtonItem = rightNavBtnWithImgName(@"预约记录", self, @selector(rightButtonAction:));
    self.dataService = [ReservationService new];
    _dataService.delegate = self;
    _searchBar.delegate = self;
    _searchBar.inputAccessoryView = [self getInputAccessoryView];
    [self startLoadingView];
    [_dataService requestDepartList];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Right Button
-(void)rightButtonAction:(id)sender
{
    NSLog(@"点击了预约记录");
    ReservationRecordListViewController *listVC = [[ReservationRecordListViewController alloc] initWithNibName:@"ReservationRecordListViewController" bundle:nil];
    [self.navigationController pushViewController:listVC animated:YES];
}

#pragma mark - Table
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_dataService.dataList count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    PolicyAdvisoryCell *cell = [[[NSBundle mainBundle] loadNibNamed:@"PolicyAdvisoryCell" owner:self options:nil] objectAtIndex:0];
    ReservationDepartListItem *item = [_dataService.dataList objectAtIndex:indexPath.row];
    cell.nameLabe.text = item.businessName;
    cell.phoneButton.hidden = YES;
    cell.advisoryButton.hidden = YES;
    cell.fileButton.hidden = YES;
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    //进入预约列表
    ReservationDepartListItem *item = [_dataService.dataList objectAtIndex:indexPath.row];
    ReservationListViewController *listVC = [[ReservationListViewController alloc] initWithNibName:@"ReservationListViewController" bundle:nil];
    listVC.unitCode = item.businessCode;
    [self.navigationController pushViewController:listVC animated:YES];
}

#pragma mark - Data Service
-(void)loadDataFinished
{
    [self stopLoadingView];
    [_tableView reloadData];
}

-(void)loadDataFaild:(NSString *)msg
{
    [self stopLoadingView];
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:msg delegate:self cancelButtonTitle:@"取消" otherButtonTitles:nil, nil];
    [alert show];
}

#pragma mark - Search Bar Delegate
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    NSLog(@"search bar input:%@",searchText);
}

-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    NSLog(@"search button clicked with input:%@",searchBar.text);
    [searchBar resignFirstResponder];
}

-(void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
{
    NSLog(@"search bar end edit with input %@",searchBar.text);
    if (searchBar.text.length == 0) {
        [self startLoadingView];
        //如果没输入内容并且点击完成，则重新刷新列表
        [_dataService requestDepartList];
    } else {
        NSString *key = searchBar.text;
        searchBar.text = @"";
        [self.view endEditing:YES];
        //进入预约列表进行搜索
        ReservationListViewController *listVC = [[ReservationListViewController alloc] initWithNibName:@"ReservationListViewController" bundle:nil];
        listVC.searchKey = key;
        [self.navigationController pushViewController:listVC animated:YES];
    }
}
@end
